import Vue from 'vue'
import VueRouter from 'vue-router'
import UserView from '../views/UserView.vue'
import MenusView from '../views/MenusView.vue'
import ClassView from '../views/ClassView.vue'
import layout from '../components/Layout.vue'
import LoginView from '../views/LoginView.vue'
Vue.use(VueRouter)
import store  from '@/store'
const routes = [
  {
    path: '/login',
    name: 'login',
    component: LoginView
  },
   {
    path: '/',
    name: 'layout',
    redirect:'/class',
    component: layout,
    children:[
      {
        path: 'class',
        name: 'class',
        component: ClassView
      },{
        path: 'menus',
        name: 'menus',
        component: MenusView
      },{
        path: 'user',
        name: 'user',
        component: UserView
      }
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to,from,next)=>{
  let token=store.state.token
//   console.log(token)
 if(to.path=='/login'){
  next()
 }else{
  if(token!=""){
    next()
  }else{
    next('/login')
  }
 }


})
//解决重复点击菜单，控制台报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
export default router
